package com.tencent.wxcloudrun.project.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencent.wxcloudrun.common.Result;
import com.tencent.wxcloudrun.common.utils.redis.RedisUtil;
import com.tencent.wxcloudrun.project.entity.User;
import com.tencent.wxcloudrun.project.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

@Controller
@ResponseBody
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    //用户登入判断
    @PostMapping("/login")
    public Result login(@RequestBody User user){

        // 获取redis中的验证码
        Object redisCode = RedisUtil.Get(user.getVerKey());
        if(redisCode==null){
            return Result.error(600,"验证码已过期");
        }
        // 判断验证码
        if (user.getVerCode()==null) {
            return Result.error(600,"验证码不能为空");
        }
        if(!redisCode.toString().equals(user.getVerCode().trim().toLowerCase())){
            return Result.error(600,"验证码不正确");
        }
        if(userService.login(user)!=null){
            return Result.success(userService.login(user));
        }
        return Result.error(600,"账号或密码不正确");
    }
    //获取用户信息
    @GetMapping("/getUser")
    public Result getUser(@RequestParam String username){
        return Result.success(userService.getUser(username));
    }
    @GetMapping("/page")
    public Result findPage(@RequestParam(defaultValue = "1") Long pageNum,
                           @RequestParam(defaultValue = "4") Long pageSize,
                           @RequestParam(defaultValue = "") String name,
                           @RequestParam(defaultValue = "") String username
    ){
        IPage<User> page = new Page<>(pageNum,pageSize);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if(!"".equals(name)){
            queryWrapper.like("name",name);
        }
        if(!"".equals(username)){
            queryWrapper.like("username",username);
        }
        return Result.success(userService.page(page,queryWrapper));
    }
}
